﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyThuVien
{
    public partial class GhiNhanMatSach : Form
    {
        public GhiNhanMatSach()
        {
            InitializeComponent();
        }
        void LoadDanhSachMatSach()
        {
            DataTable dt = PhieuMatSachBUS.LayDanhSachPhieuMatSach_BUS();
        
            if (dt.Rows.Count > 0)
            {
                dataGridViewX1.DataSource = dt;
                dataGridViewX1.Columns["MaPhieuMatSach"].HeaderText = "Mã phiếu mất sách";
                dataGridViewX1.Columns["MaSach"].HeaderText = "Mã sách";
                dataGridViewX1.Columns["TenSach"].HeaderText = "Tên sách";
                dataGridViewX1.Columns["NgayGhiNhan"].HeaderText = "Ngày ghi nhận";
                dataGridViewX1.Columns["MaDocGia"].HeaderText = "Mã đọc giả";
                dataGridViewX1.Columns["dg.HoTen"].HeaderText = "Đọc giả";
                dataGridViewX1.Columns["TienPhat"].HeaderText = "Tiền phạt";
                dataGridViewX1.Columns["nv.HoTen"].HeaderText = "Người ghi nhận";
                dataGridViewX1.Columns["MaNhanVien"].Visible = false;

            }
        }
        void LoadDanhSach_Sach()
        {
            DataTable dt = SachBUS.LayDanhSachSach_BUS();
            cbbmasach.DataSource = dt;
            cbbmasach.DisplayMember = "TenSach";
            cbbmasach.ValueMember = "MaSach";
        }
        void LoadDanhSachDocGia()
        {
            TheDocGiaBUS tdg = new TheDocGiaBUS();
            DataTable dt = tdg.LayDanhSachTheDocGia_BUS();
            cbbmadocgia.DataSource = dt;
            cbbmadocgia.ValueMember = "ID";
            cbbmadocgia.DisplayMember = "HoTen";
        }
        void LoadDanhSachNhanVien()
        {
            DataTable dt = NhanVienBUS.LayDSNhanVienTheoBoPhan(1);
            cbbnguoighinhan.DataSource = dt;
            cbbnguoighinhan.DisplayMember = "HoTen";
            cbbnguoighinhan.ValueMember = "MaNhanVien";
        }
        private void GhiNhanMatSach_Load(object sender, EventArgs e)
        {
            LoadDanhSachMatSach();
            LoadDanhSach_Sach();
            LoadDanhSachDocGia();
            LoadDanhSachNhanVien();
        }
        int maPMS = -1;
        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewX1.SelectedRows.Count > 0 && dataGridViewX1.SelectedRows[0].IsNewRow == false)
            {
                DataGridViewRow dr = dataGridViewX1.SelectedRows[0];
                txttienphat.Text = dr.Cells["TienPhat"].Value.ToString();
                cbbnguoighinhan.SelectedValue = int.Parse(dr.Cells["MaNhanVien"].Value.ToString());
                cbbmasach.SelectedValue = int.Parse(dr.Cells["MaSach"].Value.ToString());
                cbbmadocgia.SelectedValue = int.Parse(dr.Cells["MaDocGia"].Value.ToString());
                dtngayghinhan.Value = DateTime.Parse(dr.Cells["NgayGhiNhan"].Value.ToString());
                maPMS=int.Parse(dr.Cells["MaPhieuMatSach"].Value.ToString());
            }
        }

        private void btlammoi_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupPanel1.Controls)
            {
                if (ctrl.GetType() != typeof(DevComponents.DotNetBar.ButtonX) && ctrl.GetType() != typeof(DevComponents.DotNetBar.LabelX))
                {
                    ctrl.ResetText();
                }
            }
        }

        private void btlapphieu_Click(object sender, EventArgs e)
        {
            PhieuMatSachDTO pmsDTO = new PhieuMatSachDTO();
            pmsDTO.Madocgia = int.Parse(cbbmadocgia.SelectedValue.ToString());
            pmsDTO.Masach = int.Parse(cbbmasach.SelectedValue.ToString());
            pmsDTO.Ngayghinhan = dtngayghinhan.Value;
            pmsDTO.Tienphat = int.Parse(txttienphat.Text);
            pmsDTO.Nguoighinhan = int.Parse(cbbnguoighinhan.SelectedValue.ToString());
            PhieuMatSachBUS.ThemPhieuMatSach_BUS(pmsDTO);
            LoadDanhSachMatSach();
        }

        private void btxoaphieu_Click(object sender, EventArgs e)
        {
            PhieuMatSachBUS.XoaPhieuMatSach_BUS(maPMS);
            LoadDanhSachMatSach();
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btcapnhat_Click(object sender, EventArgs e)
        {
            PhieuMatSachDTO pmsDTO = new PhieuMatSachDTO();
            pmsDTO.Madocgia = int.Parse(cbbmadocgia.SelectedValue.ToString());
            pmsDTO.Masach = int.Parse(cbbmasach.SelectedValue.ToString());
            pmsDTO.Ngayghinhan = dtngayghinhan.Value;
            pmsDTO.Tienphat = int.Parse(txttienphat.Text);
            pmsDTO.Nguoighinhan = int.Parse(cbbnguoighinhan.SelectedValue.ToString());
            pmsDTO.Maphieumatsach = maPMS;
            if(maPMS!=-1)
            PhieuMatSachBUS.CapNhatPhieuMatSach_BUS(pmsDTO);
            LoadDanhSachMatSach();
        }

        private void bttimphieu_Click(object sender, EventArgs e)
        {
            string param = "";
            if (cbbmadocgia.SelectedIndex > -1)
            {
                param += " and dg.MaDocGia=" + cbbmadocgia.SelectedValue.ToString();
            }
            if (cbbmasach.SelectedIndex > -1)
            {
                param += " and s.MaSach=" + cbbmasach.SelectedValue.ToString();

            }
            if (cbbnguoighinhan.SelectedIndex > -1)
            {
                param += " and nv.MaNhanVien=" + cbbnguoighinhan.SelectedValue.ToString();
            }
            DataTable dt = PhieuMatSachBUS.TimPhieuMatSach_BUS(param);

            if (dt.Rows.Count > 0)
            {
                dataGridViewX1.DataSource = dt;

            }
            else
            {
                MessageBox.Show("Không có dữ liệu");
            }
           
        }

       

        
        
    }
}